#include "RenderPch.h"

#include "CameraManager.h"
#include "RenderFacade.h"

#include "CameraNavigation.h"

CameraManager::CameraManager() :
m_pCurCamera( NULL )
{
}

CameraManager::~CameraManager(void)
{
	std::map<CameraType, CameraBase*>::iterator it = m_cameraMap.begin();
	for( ; it != m_cameraMap.end(); it++ )
	{
		SAFE_DELETE( it->second );
	}

	m_cameraMap.clear();
}

void CameraManager::Update( float dTime )
{
	if( m_pCurCamera )
	{
		m_pCurCamera->Update( dTime );

		Matrix viewTransform, projTransform;
		m_pCurCamera->GetViewTransform( viewTransform );
		m_pCurCamera->GetProjectionTransform( projTransform );

		RenderFacade::GetInstance()->SetTransform( D3DTS_VIEW, &viewTransform );
		RenderFacade::GetInstance()->SetTransform( D3DTS_PROJECTION, &projTransform );
	}
}

void CameraManager::HandleEvent( UINT message, WPARAM wParam, LPARAM lParam )
{
	if( m_pCurCamera )
	{
		m_pCurCamera->HandleEvent( message, wParam, lParam );
	}
}

void CameraManager::AddCamera( CameraType cameraType )
{
	std::map<CameraType, CameraBase*>::iterator it = m_cameraMap.find( cameraType );
	if( it == m_cameraMap.end() )
	{
		CameraBase * camera = NULL;
		switch( cameraType )
		{
		case NAVIGATION_CAMERA:
			{
				camera = new CameraNavigation();
			}
			break;
		}

		ProjectionInfo info;
		camera->SetProjectionInfo( info );
		m_cameraMap[cameraType] = camera;
	}
}

BOOL CameraManager::DeleteCamera( CameraType cameraType )
{
	std::map<CameraType, CameraBase*>::iterator it = m_cameraMap.find( cameraType );
	if( it != m_cameraMap.end() )
	{
		SAFE_DELETE( it->second );
		m_cameraMap.erase( it );

		return TRUE;
	}

	return FALSE;
}

BOOL CameraManager::SetCurCamera( CameraType cameraType )
{
	std::map<CameraType, CameraBase*>::iterator it = m_cameraMap.find( cameraType );
	if( it != m_cameraMap.end() )
	{
		m_pCurCamera = it->second;

		return TRUE;
	}

	return FALSE;
}
